let province = ['-请选择-', '上海', '云南', '内蒙古', '北京', '台湾', '吉林', '四川', '天津', '宁夏', '安徽', '山西', '广东', '广西', '新疆',
    '江苏', '江西', '河北', '河南', '浙江', '海南', '湖北', '湖南', '澳门', '甘肃', '福建', '西藏', '贵州', '辽宁', '重庆', '陕西',
    '青海', '香港', '黑龙江'];
let cludata = null;
let Cluster_table = [];
let start = 0;//开始索引
let end = 0;//结束索引
let All = 1;//全部分页数量
let last = 0;//去除整数后剩余页数
let now_ = 1;//目前所在页
let updateTime = '-请选择-';//数据最近更新时间
$("#tab_title").hide(); //初始隐藏
$("#bulast").hide(); //初始隐藏
$("#bunext").hide(); //初始隐藏

function addToList() { //向选择列表添加相应想
    for (let i = 0; i < province.length; i++) {
        $("#select_p").append("<option value=" + i + ">" + province[i] + "</option>");
    }
}

function getCluster() { //使用ajax获取聚类结果
    $.ajax({
        type: 'get',
        url: '/getClusterData',
        success: function (cdata) {
            cludata = cdata;
            console.log(cludata);
        }, error: function () {
            console.log("这个接口挂了");
        }
    });
}

function jsonHandle(pname) { //通过此方法寻找与选择省份数据相似的省份
    let select_tag = null;
    let table_res = [];
    let temp_name = null;
    let tag = null;
    updateTime = cludata['time'];
    for (let i = 0; i < cludata['res'].length; i++) {//获取所选省份的标记
        temp_name = Object.keys(cludata['res'][i])[0];
        if (temp_name == pname) {
            select_tag = cludata['res'][i][temp_name]['tag'];
            console.log('选中tag为' + select_tag)
            break;
        }
    }


    for (let i = 0; i < cludata['res'].length; i++) {//获取具有以上标记的省份
        temp_name = Object.keys(cludata['res'][i])[0];
        tag = cludata['res'][i][temp_name]["tag"];
        if (select_tag == tag && temp_name != pname) {
            table_res.push(cludata['res'][i])
        }
    }
    console.log(table_res)
    return table_res;
}

function getSelected() {//获取所选，并初始化table数据
    $("#sub").click(function () {
        $("#table_its").empty();
        $("#bulast").hide(); //初始隐藏
        $("#bunext").hide(); //初始隐藏
        let obj = $("#select_p option:selected");
        let index_select = parseInt(obj.val());
        if (index_select != 0 && cludata != null) {
            //$("#bulast").show();
            //$("#bunext").show();
            $("#table_its").empty();
            $("#tab_title").text('与' + province[index_select] + "数据相似省份(数据根据以下三项进行聚类)");
            $("#tab_title").show();
            let selectName = province[index_select];//获取所选省份
            Cluster_table = jsonHandle(selectName);//获取相似列表
            $("#updateTime").text("最近聚类数据更新时间: "+updateTime);
            let arr_length = Cluster_table.length;
            let temp = arr_length % 5;
            start = 0;
            last = 0;
            now_ = 1;
            if (temp == 0) {
                All = arr_length / 5;
                end = start+4;
                console.log("All1:"+All);
            }else{
                All = Math.ceil(arr_length / 5);
                console.log("All2:"+All);
                last = temp;
                if(All == 1){
                    end = last-1;
                }else{
                    end = start+4;
                }
            }
            if(All > 1){
                $("#bunext").show();
            }
            showTable(start, end);
        }else{
            $("#tab_title").hide(); //选择项为“未选择时”，隐藏标题框
        }
    });
}

function click_Button() {//next和last按钮的选择，负责对数据索引更新
    $("#bunext").click(function () {
        now_++;//当前页数加一
        if(now_ == All) {//当前页数等于全部页数
            if(last==0){//余数为零
                start = end+1;
                end = start + 4;
            }else{//余数不为零
                start = end+1;
                end  = start + last -1;
            }
            $("#bunext").hide();
        }else{//不等于全部页数
            start = start + 4;
            end = end +4;
        }
        showTable(start, end);
    });

    $("#bulast").click(function () {
        now_--;
        if(now_+1 == All && last !=0){//从最后一个回到上一页 1.余数不为零
            end = end - last;
            start  = end - 4;
        }
        if(now_+1 == All && last ==0){//2.余数为零
            end = start - 1;
            start  = end - 4;
        }
        if(now_ == 1){ //第一页
            $("#bulast").hide();
            start = 0;
            end = start +4;
        }
        showTable(start, end);
    });

}

function showTable(start, end) {//表格数据渲染，按钮控制
    $("#table_its").empty();//清空tbody区域
    if (now_ > 1) { //当前页数大于1时，显示上一页
        $("#bulast").show();
    }

    if (now_ < All) { //当前页数小于1时，显示下一页
        $("#bunext").show();
    }

    let tp = null;//获取省份名字
    let tdata = null;//获取省份数据
    if(All != 0){
        for (let i = start; i <= end; i++) {
            tp = Object.keys(Cluster_table[i])[0];
            tdata = Cluster_table[i][tp]['pdata'];
            $("#table_its").prepend("<tr><td>" + tp + "</td> <td>" + tdata[0] + "</td> <td>" + tdata[2] + "</td> <td>" + tdata[1] + "</td></tr>");
        }
    }
}

getCluster();
addToList();
getSelected();
click_Button();



